<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class VipController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.vip.add');
    }

    //insert
    public function insert(Request $request)
    {
    	//数据
    	$data = $request -> except('_token');

        
    	$data['cate'] = $data['price'] * $data['data'];
    	//添加数据至数据库
    	$res = DB::table('vip') -> insert($data);

    	//判断是否添加成功
    	if($res)
    	{
    		return redirect('/admin/vip/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }

    //index
    public function index()
    {
    	$data = DB::table('vip') -> get();
    	return view('admin.vip.index',['data' => $data]);
    }

    //edit
    public function edit($id)
    {
    	$data = DB::table('vip') -> where('id',$id) -> first();
    	return view('admin/vip/edit',['data' => $data]);
    }

    //update
    public function update(Request $request,$id)
    {
    	$data = $request -> except('_token');
    	$data['cate'] = $data['price'] * $data['data'];

    	$res = DB::table('vip') -> where('id',$id) -> update($data);
    	if($res)
        {
            return redirect('admin/vip/index') -> with(['info' => '更新成功']);
        }else
        {
            return back() -> with(['info' => '更新失败']);
        }
    }

    //delete
    public function delete($id)
    {
    	$res = DB::table('vip') -> delete($id);

    	if($res)
    	{
    		return redirect('admin/vip/index') -> with(['info' => '删除成功']);
    	}else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}

    }

}
